<%-- 
    Document   : AddToCart
    Created on : May 23, 2014, 11:21:27 AM
    Author     : tuanna
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <%
        String warning = "";
        String pid = request.getParameter("pid");
        String pname = request.getParameter("pname");
        float price = Float.parseFloat(request.getParameter("price"));
        
        Cookie co[]=request.getCookies();
        Cookie c;
        boolean status = false;
        
        for(Cookie cookie : co){
            if(cookie.getName().equals("product"+pid)){
                    String pro=cookie.getValue();
                    String p[]=pro.split("\\|");
                    int count = Integer.parseInt(p[2]);
                    count++;
                    c=new Cookie("product"+pid, pid+"|"+pname+"|"+count+"|"+price*count);
                    warning="You just add one "+pname+"into Shopping Cart";
                    response.addCookie(c);
                    status=true;
            }
        }
        if(status==false){
                c=new Cookie("product"+pid, pid+"|"+pname+"|1|"+price);
                warning="You just add one "+pname+"into Shopping Cart";
                response.addCookie(c);
                status=true;
            }
    %>
    <body>
        <form>
            <table>
                <tr><th><h3><%=warning%></h3></tr>
            <tr><th><h4><a href="showCart.jsp">Show Shopping Cart</h4></tr>
            </table>
        </form>
    </body>
</html>
